package com.kingsoft.dc.khaos.module.spark.request.model

import java.sql.{Connection, DriverManager}

import com.kingsoft.dc.khaos.util.Logging

import scala.collection.mutable

/**
  * Created by jing on 19/4/20.
  */
class JdbcConnectEntity() extends Serializable with Logging {
  private var url: String = ""
  private var user: String = ""
  private var password: String = ""
  private var table: String = ""
  private var schema: String = ""


  def this(url: String, user: String, password: String, schema: String, table: String) {
    this
    this.url = url
    this.user = user
    this.password = password
    this.table = table
    this.schema = schema
  }

  def getSchemaName(): String = {
    schema
  }

  def getTableName(): String = {
    table
  }

  /**
    * 获取MPP表结构(非分表)
    *
    * @return
    */
  def getMppTableSchema(loginTimeout: Int): mutable.HashMap[String, StructFieldEntity] = {
    var conn: Connection = null
    var colMap = mutable.HashMap[String, StructFieldEntity]()
    try {
      DriverManager.setLoginTimeout(loginTimeout)
      Class.forName("org.postgresql.Driver")
      conn = DriverManager.getConnection(url, user, password)
      log.info("正在查询表元数据信息...")
      val dbmd = conn.getMetaData()
      // 转义 _和% 通配符
      val escape = dbmd.getSearchStringEscape
      val db_name = schema.replace("_", escape + "_").replace("%", escape + "%")
      logInfo("dbname===》" + db_name)
      val tbl_name = table.replace("_", escape + "_").replace("%", escape + "%")
      logInfo("tbname===》" + tbl_name)
      val rs = dbmd.getColumns(conn.getCatalog(), db_name, tbl_name, null)
      while (rs.next()) {
        val tblSchema = new StructFieldEntity(rs.getString("COLUMN_NAME"), rs.getString("TYPE_NAME"), rs.getInt("COLUMN_SIZE"), rs.getInt("DECIMAL_DIGITS"), rs.getInt("ORDINAL_POSITION"))
        logInfo("COLUMN_NAME===>" + rs.getString("COLUMN_NAME"))
        colMap += (rs.getString("COLUMN_NAME") -> tblSchema)
      }
    } catch {
      case e: Exception =>
        e.printStackTrace()
    } finally {
      if (conn != null) {
        conn.close()
      }
    }
    log.info("查询表元数据信息结束...")
    colMap
  }

  /**
    * 获取MPP表结构(分表)
    *
    * @return
    */
  def getMppTrueTableSchema(tblName: String,
                            loginTimeout: Int): mutable.HashMap[String, StructFieldEntity] = {
    var conn: Connection = null
    var colMap = mutable.HashMap[String, StructFieldEntity]()
    try {
      DriverManager.setLoginTimeout(loginTimeout)
      Class.forName("org.postgresql.Driver")
      conn = DriverManager.getConnection(url, user, password)
      log.info("正在查询表元数据信息...")
      val dbmd = conn.getMetaData()
      // 转义 _和% 通配符
      val escape = dbmd.getSearchStringEscape
      val db_name = schema.replace("_", escape + "_").replace("%", escape + "%")
      val tbl_name = tblName.replace("_", escape + "_").replace("%", escape + "%")

      val rs = dbmd.getColumns(conn.getCatalog(), db_name, tbl_name, null)
      while (rs.next()) {
        val tblSchema = new StructFieldEntity(rs.getString("COLUMN_NAME"), rs.getString("TYPE_NAME"), rs.getInt("COLUMN_SIZE"), rs.getInt("DECIMAL_DIGITS"), rs.getInt("ORDINAL_POSITION"))
        colMap += (rs.getString("COLUMN_NAME") -> tblSchema)
      }
    } catch {
      case e: Exception =>
        e.printStackTrace()
    } finally {
      if (conn != null) {
        conn.close()
      }
    }
    log.info("查询表元数据信息结束...")
    colMap
  }
}
